//index.js
//获取应用实例
const app = getApp();
let common = require('../../utils/util.js');

Page({
  data: {
    userInfo: {},
    card:{
      card_nickname:'null',
      card_avatar:'null'
    },
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function() {
    // wx.clearStorage();
    let that = this;
    let info = app.globalData.userInfo;
    let card = wx.getStorageSync('card');
    console.log(info);
    if (info) {
      that.setData({
        userInfo: info,
        card:card,
        hasUserInfo: true
      });
      console.log(that.data.hasUserInfo);
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.saveCardInfo(res);
        this.setData({
          userInfo: res.userInfo,
          card: card,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          this.saveCardInfo(res);
          this.setData({
            userInfo: res.userInfo,
            card: card,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onShow: function() {
    let that = this;
    wx.getStorage({
      key: 'card',
      success: function(res) {
        console.log(res.data.card_id);
        console.log('res');
        that.setData({
          userInfo: app.globalData.userInfo,
          card: res.data,
          //mobile: data.mobile.substr(0, 3) + '-' + data.mobile.substr(3, 4) + '-' + data.mobile.substr(7),
          hasUserInfo: res.data.card_id == undefined ? false : true
        });
      },
      fail:function(){
        console.log('error');
        
      }
    })
  },
  // 展示信息
  showData: function(data) {
    let hasUserInfo = data.card_name== null ? false : true;
    this.setData({
      card: data,
      // mobile: data.mobile.substr(0, 3) + '-' + data.mobile.substr(3, 4) + '-' + data.mobile.substr(7),
      hasUserInfo: hasUserInfo
    });
  },
  // 获取用户信息
  getUserInfo: function(e) {
    if (!e.detail.userInfo) {
      return;
    }
    app.globalData.userInfo = e.detail.userInfo;
   
    this.saveCardInfo(e.detail);
    let card = wx.getStorageSync('card');
    this.setData({
      userInfo: e.detail.userInfo,
      card:card,
      hasUserInfo: true
    })
  },
  // 保存用户信息
  saveCardInfo: function(data) {
    let self = wx.getStorageSync('card');
    // console.log(data.rawData);
   if (self.card_nickname == null && data.rawData) {
     wx.setStorageSync('card', self);
      common.request(common.urls.saveCard, {
        user: data.rawData,
        token:wx.getStorageSync('token')
      }, 'POST', function(res) {
        
        if (res.data.code==1) {
          // console.log(res);
          // self.card_nickname = res.data.data.card_nickname;
          // self.card_avatar = res.data.data.card_avatar;
          // self.card_id = res.data.data.card_id;
          wx.setStorageSync('card', res.data.data);
              this.setData({
                    card: res.data.data,
                    hasUserInfo: true
              })
        }
      });
    }
//     console.log(self);


  },
  // 创建名片
  create: function(e) {
    let self = this.data.card;
    if (self.card_nickname == null) {
      wx.showModal({
        title: '温馨提示',
        content: '请先点击上面按钮获取您的头像~_~',
        showCancel: false
      });
      return;
    }
    wx.navigateTo({
      url: '../user/user',
    });
  },
  // 拨打电话
  makeFhoneCall: function() {
    console.log(this.data);
    wx.makePhoneCall({
      phoneNumber: this.data.card.card_mobile,
    });
  },
  onShareAppMessage: function() {
    let self = wx.getStorageSync('card');
    if (self.card_nickname) {
      return {
        title: '添加我为好友，联系更方便',
        path: '/pages/friend/friend?card_id=' + self.card_id
      }
    } else {
      return {
        title: '添加我为好友，联系更方便',
        path: '/pages/index/index'
      }
    }
  }
})